<template>
  <div>
    门诊病历首页
    <!-- 误关闭按钮 -->
    <el-divider content-position="center">已登记病人</el-divider>
    <el-table :data="registRecord" style="width: 100%">
      <el-table-column prop="ptId" label="编号" width="180"></el-table-column>
      <el-table-column prop="ptName" label="名字" width="180"></el-table-column>
      <el-table-column>
        <template slot-scope="scope">
          <el-button size="mini" @click="selectRegistClicked(scope.$index, scope.row)">选择</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="registHandleSizeChange"
        @current-change="registHandleCurrentChange"
        :current-page.sync="registCurrentPageNum"
        :page-sizes="[1, 5, 10]"
        :page-size="registPageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="registTotalRecord"
      ></el-pagination>
    </div>
    <el-divider content-position="center">待确诊病人</el-divider>
    <el-table :data="diagRecord" style="width: 100%">
      <el-table-column prop="ptId" label="编号" width="180"></el-table-column>
      <el-table-column prop="ptName" label="名字" width="180"></el-table-column>
      <el-table-column>
        <template slot-scope="scope">
          <el-button size="mini" @click="selectDiagClicked(scope.$index, scope.row)">选择</el-button>
          <el-button size="mini" @click="FinishClicked(scope.$index, scope.row)">诊毕</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加" :visible.sync="addDialogVisible">
      <el-form :model="userForm" ref="form" label-width="100px">
        <el-form-item label="疾病分类" prop="disease">
          <el-select filterable clearable v-model="userForm.medicalAdvice" placeholder="请选择疾病分类">
            <el-option
              v-for="item in disease"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="非药物分类开具" prop="nonDrugs">
          <el-select multiple v-model="formNonDrugs" placeholder="请选择非药物开具">
            <el-option
              v-for="item in nonDrugs"
              :key="item.nonDrugId"
              :label="item.nonDrugName"
              :value="item.nonDrugId"
            ></el-option>
          </el-select>
          <el-button type="primary" @click="sendNonDrug">开具检查</el-button>
        </el-form-item>
        <el-form-item label="药物开具" prop="nonDrugs">
          <el-select multiple filterable clearable v-model="formDrugs" placeholder="请选择药物开具">
            <el-option
              v-for="item in drugs"
              :key="item.drugId"
              :label="item.drugName"
              :value="item.drugId"
            ></el-option>
          </el-select>
          <el-button type="primary" @click="sendDrug">开具药物</el-button>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="save">提交</el-button>
      </span>
    </el-dialog>
    <div class="block">
      <el-pagination
        @size-change="diagHandleSizeChange"
        @current-change="diagHandleCurrentChange"
        :current-page.sync="diagCurrentPageNum"
        :page-sizes="[1, 5, 10]"
        :page-size="diagPageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="diagTotalRecord"
      ></el-pagination>
    </div>
    <el-divider content-position="center">诊断历史</el-divider>
    <el-table :data="oldDiagRecord" style="width: 100%">
      <el-table-column prop="ptId" label="编号" width="180"></el-table-column>
      <el-table-column prop="ptName" label="名字" width="180"></el-table-column>
      <el-table-column prop="diagId" label="诊断号" width="180"></el-table-column>
      <!-- <el-table-column prop="diseaseHistory" label="" width="180"></el-table-column> -->
      <el-table-column prop="medicalAdvices" label="诊断" width="180"></el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="oldDiagHandleSizeChange"
        @current-change="oldDiagHandleCurrentChange"
        :current-page.sync="oldDiagCurrentPageNum"
        :page-sizes="[1, 5, 10]"
        :page-size="oldDiagPageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="oldDiagTotalRecord"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import {
  getRegistPage,
  getDiagPage,
  selectRegist,
  selectDiag,
  getDisease,
  getAllDrug,
  getAllNoneDrug,
  updateDiag,
  addOneND,
  addOneD,
  finishDiag,
  getAllRecords,
  selectOldDiag
} from "@/api/recordHome.js";
import { getUserId } from "@/utils/LocalStorage.js";
export default {
  mounted() {
    this.loadPageData();
    this.loadDisease();
  },
  data() {
    return {
      addDialogVisible: false,
      registPageSize: 1,
      diagPageSize: 1,
      oldDiagPageSize: 1,
      registTotalRecord: 3,
      diagTotalRecord: 3,
      oldDiagTotalRecord: 3,
      registCurrentPageNum: 1,
      diagCurrentPageNum: 1,
      oldDiagCurrentPageNum: 1,
      registRecord: [],
      diagRecord: [],
      disease: [],
      drugs: [],
      nonDrugs: [],
      formNonDrugs: [],
      formDrugs: [],
      userForm: {
        medicalAdvice: ""
      },
      oldDiagRecord: []
    };
  },
  methods: {
    loadPageData() {
      let registPageParam = {
        userId: parseInt(JSON.parse(getUserId())),
        curPage: this.registCurrentPageNum,
        pageSize: this.registPageSize
      };
      console.log("send getPage: ", registPageParam);
      getRegistPage(registPageParam).then(response => {
        if (response.data.code == 0) {
          console.log("get getRegistPage response ", response);
          this.registRecord = [];
          response.data.data.records.forEach(element => {
            this.registRecord.push({
              ptName: element.ptName,
              ptId: parseInt(element.ptId),
              registId: parseInt(element.registId)
            });
          });
          this.registTotalRecord = parseInt(response.data.data.total);
          console.log("get registTotalRecord: ", this.registTotalRecord);
          console.log("get registPage: ", this.registRecord);
        }
      });
      let diagPageParam = {
        /* userid */
        userId: parseInt(JSON.parse(getUserId())),
        curPage: this.diagCurrentPageNum,
        pageSize: this.diagPageSize
      };
      console.log("send  diag getPage: ", diagPageParam);
      getDiagPage(diagPageParam).then(response => {
        if (response.data.code == 0) {
          console.log("get getDiagPage response:", response);
          this.diagRecord = [];
          response.data.data.records.forEach(element => {
            this.diagRecord.push({
              ptName: element.ptName,
              ptId: parseInt(element.ptId),
              diagId: parseInt(element.diagId)
            });
          });
          this.diagTotalRecord = parseInt(response.data.data.total);
          console.log("all diagPage: ", this.diagRecord);
        }
      });
      let oldPageParm = {
        userId: parseInt(JSON.parse(getUserId())),
        curPage: this.oldDiagCurrentPageNum,
        pageSize: this.oldDiagPageSize
      };
      getAllRecords(oldPageParm).then(response => {
        if (response.data.code == 0) {
          console.log(
            "get AllOldRecords: ",
            response,
            " and  num: ",
            response.data.data.records.length
          );
          this.oldDiagRecord = [];
          this.oldDiagTotalRecord = parseInt(response.data.data.total);
          response.data.data.records.forEach(element => {
            let tempOldOne = {};
            tempOldOne.diagId = element.diagId;
            tempOldOne.ptId = element.ptId;
            tempOldOne.ptName = element.ptName;
            tempOldOne.medicalAdvices = element.medicalAdvice;
            this.oldDiagRecord.push(tempOldOne);
          });
        }
      });
      getAllDrug().then(response => {
        if (response.data.code == 0) {
          this.drugs = [];
          console.log("get nonDrugs: ", response.data.data);
          response.data.data.forEach(element => {
            this.drugs.push({ drugId: element.id, drugName: element.drugName });
          });
        }
        console.log("all drugs:", this.drugs);
      });
      getAllNoneDrug().then(response => {
        if (response.data.code == 0) {
          this.nonDrugs = [];
          response.data.data.forEach(element => {
            this.nonDrugs.push({
              nonDrugId: element.id,
              nonDrugName: element.nonDrugName
            });
          });
        }
        console.log("all nonDrugs:", this.nonDrugs);
      });
    },
    loadDisease() {
      console.log("loadDisease");
      getDisease().then(response => {
        if (response.data.code == 0) {
          console.log("loadDisease response,", response.data.data);
          // this.disease = response.data.data;
          response.data.data.forEach(element => {
            this.disease.push({
              value: element.diseaseName,
              label: element.diseaseName
            });
          });
          console.log(response.data.data);
        }
      });
      // this.disease = [
      //   { value: "感冒1", label: "感冒1" },
      //   { value: "感冒2", label: "感冒2" },
      //   { value: "感冒3", label: "感冒3" }
      // ];
      this.loadPageData();
    },
    // 接诊患者
    selectRegistClicked(index, row) {
      this.$confirm("确定要接诊此用户吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.userForm.id = row.registId;
          this.userForm.ptId = row.ptId;
          this.formNonDrugs = [];
          // this.addDialogVisible = true;
          /* 在此处填写接诊 */
          /* 这里的userId也有问题 */
          let selectRegistParam = {
            registId: row.registId,
            userId: parseInt(JSON.parse(getUserId()))
          };
          console.log("send selectRegist: ", selectRegistParam);
          selectRegist(selectRegistParam).then(response => {
            if (response.data.code == 0) {
              console.log("selectRegist success");
              // 函数本身就返回bool,无需要操作了
              this.loadPageData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消接诊"
          });
        });
    },
    // 接诊检查后的患者
    selectDiagClicked(index, row) {
      this.$confirm("确定要接诊此用户吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.userForm.id = row.diagId;
          this.userForm.ptId = row.ptId;
          this.formNonDrugs = [];
          this.addDialogVisible = true;
          let selectDiagParam = {
            diagId: row.diagId
          };
          console.log("send selectDiag: ", selectDiagParam);
          selectDiag(selectDiagParam).then(response => {
            if (response.data.code == 0) {
              console.log("selectDiag success");
              this.loadPageData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消接诊"
          });
        });
    },
    FinishClicked(index, row) {
      console.log("诊断完毕!");
      console.log("now row ", row);
      let FinishParam = { diagId: parseInt(row.diagId) };
      console.log("send finish param ", FinishParam);
      finishDiag(FinishParam).then(response => {
        if (response.data.code == 0) {
          this.$message("确诊完毕!");
          this.loadPageData();
        }
      });
    },
    registHandleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.registPageSize = val;
      // 更新页数
      this.loadPageData();
    },
    diagHandleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.diagPageSize = val;
      // 更新页数
      this.loadPageData();
    },
    registHandleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.registCurrentPageNum = val;
      // 更新页数
      this.loadPageData();
    },
    diagHandleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.diagCurrentPageNum = val;
      // 更新页数
      this.loadPageData();
    },
    oldDiagHandleSizeChange(val) {
      console.log(`当前页: ${val}`);
      this.oldDiagPageSize = val;
      // 更新页数
      this.loadPageData();
    },
    oldDiagHandleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.oldDiagCurrentPageNum = val;
      // 更新页数
      this.loadPageData();
    },
    save() {
      this.addDialogVisible = false;
      let updateParam = {
        id: this.userForm.id,
        medicalAdvice: this.userForm.medicalAdvice
      };
      console.log("send updateDiag ", updateParam);
      updateDiag(updateParam).then(response => {
        if (response.data.code == 0) {
          console.log("updateDiag success!");
        }
      });
      this.loadPageData();
    },
    sendNonDrug() {
      console.log("userform before sendNonDrug", this.userForm);
      console.log("formNonDrugs before sendNonDrug", this.formNonDrugs);
      this.formNonDrugs.forEach(item => {
        let nonDrugParam = {
          diagId: parseInt(this.userForm.id),
          nonDrugId: parseInt(item),
          nonDrugNum: 1
        };
        console.log("send nonDrugParam: ", nonDrugParam);
        addOneND(nonDrugParam).then(response => {
          if (response.data.code == 0) {
            console.log("send nonDrugParam success");
          }
          this.loadPageData();
        });
      });
    },
    sendDrug() {
      this.formDrugs.forEach(item => {
        let drugParam = {
          diagId: parseInt(this.userForm.id),
          drugId: parseInt(item),
          drugNum: 1
        };
        console.log("send DrugParam: ", drugParam);
        addOneD(drugParam).then(response => {
          if (response.data.code == 0) {
            console.log("send DrugParam success");
          }
        });
      });
    }
  }
};
</script>

<style>
</style>